package webapp.my.com.retrofit2okhttprjavademo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import webapp.my.com.retrofit2okhttprjavademo.API.ApiModel;
import webapp.my.com.retrofit2okhttprjavademo.Bean.TokenBean;

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.tx1)
    TextView tx1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
    }

    @OnClick({R.id.btn1, R.id.btn2,R.id.btn3,R.id.btn4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                getToken();
                break;
            case R.id.btn2:
                getToken_String();
                break;
            case R.id.btn3:
                getToken_String1();
                break;
            case R.id.btn4:
                Intent intent = new Intent(MainActivity.this,RXJavaActivity.class);
                startActivity(intent);
                break;
        }
    }
    public void getToken(){
        ApiModel.getToken("YXA67T08EhEeaT05lryBPrMQ", "YXA6Ib0tbAHpZlQC53XsuClLehwT9FY", new Observer<TokenBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i("mylog","onSubscribe");
            }

            @Override
            public void onNext(TokenBean value) {
                Log.i("mylog","onNext");
                tx1.setText("1"+value.toString()+"");
            }

            @Override
            public void onError(Throwable e) {
                Log.i("mylog","onError");
            }

            @Override
            public void onComplete() {
                Log.i("mylog","onComplete");
            }
        });
    }
    public void getToken_String(){
        Map<String,String> map = new HashMap<>();
        map.put("appid","YXA67T08EhEeaT05lryBPrMQ");
        map.put("appsecret","YXA6Ib0tbAHpZlQC53XsuClLehwT9FY");
        ApiModel.getToken_string(map, new Observer<TokenBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i("mylog","onSubscribe");
            }

            @Override
            public void onNext(TokenBean value) {
                Log.i("mylog","onNext");
                tx1.setText("2"+value.toString()+"");
            }

            @Override
            public void onError(Throwable e) {
                Log.i("mylog","onError");
            }

            @Override
            public void onComplete() {
                Log.i("mylog","onComplete");
            }
        });
    }
    public void getToken_String1(){
        ApiModel.getToken_string1("YXA67T08EhEeaT05lryBPrMQ","YXA6Ib0tbAHpZlQC53XsuClLehwT9FY", new Observer<TokenBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i("mylog","onSubscribe");
            }

            @Override
            public void onNext(TokenBean value) {
                Log.i("mylog","onNext");
                tx1.setText("3"+value.toString()+"");
            }

            @Override
            public void onError(Throwable e) {
                Log.i("mylog","onError");
            }

            @Override
            public void onComplete() {
                Log.i("mylog","onComplete");
            }
        });
    }
}
